<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <div id="app">
      <h3>小黑的书架</h3>
      <ul>
        <!-- 
            :key="唯一值"  -----简写
            v-bind:key="唯一值"    给元素添加的唯一标识，便于Vue进行列表项的正确排序复用
            ● key 的值只能是 字符串 或 数字类型 
            ● key的值必须具有唯一性
        -->
        <li v-for="item in booksList" :key="item.id">
        <!-- <li v-for="item in booksList"> -->
          <span>{{item.name}}</span>
          <span>{{item.author}}</span>
          <button @click="dele(item.id)">删除</button>
        </li>
      </ul>
    </div>
    <script src="https://cdn.jsdelivr.net/npm/vue@2/dist/vue.js"></script>
    <script>
      const app = new Vue({
        el: "#app",
        data: {
          booksList: [
            { id: 1, name: "《红楼梦》", author: "曹雪芹" },
            { id: 2, name: "《西游记》", author: "吴承恩" },
            { id: 3, name: "《水浒传》", author: "施耐庵" },
            { id: 4, name: "《三国演义》", author: "罗贯中" },
          ],
        },
        methods: {
          dele(id) {
            // filter 过滤掉id相同的数据
            // 注意：filter不修改原来的数据
            this.booksList = this.booksList.filter((item) => item.id !== id);
          },
        },
      });
    </script>
  </body>
</html>
